package com.tron.dobest.java.tronnio;


import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TronFileChannelWrite {
    public static void main(String[] args) throws IOException {
        String str = "hell world";
        FileOutputStream fileOutputStream = new FileOutputStream(getPath(TronFileChannelWrite.class) + "test01.txt");
        FileChannel fileChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        byteBuffer.flip();
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }

    public static String getPath(Class clazz) {
        String pre = "src/main/java/";
        Package aPackage = clazz.getPackage();
        String name = aPackage.getName();
        String newName = pre + name.replaceAll("\\.", "/")+"/";
        return newName;
    }
}
